/* $Id: RR.java 88 2008-01-19 15:49:18Z osborn.steven $
 * 
 * Undroid Package Manager
 *
 * Copyright 2007 Steven Osborn
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.bitsetters.undroid.common;

import java.lang.reflect.Field;

import org.xmlpull.v1.XmlPullParser;

import com.bitsetters.undroid.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;

/**
 * Provides runtime access to R.class via reflection.
 * 
 * @author Steven Osborn - http://steven.bitsetters.com
 */
public class RR {

	public static final String TAG = "RR";

	/**
	 * Answers a XMLPullParser object for the provided resource name
	 * 
	 * @param context
	 * @param resource
	 * @return
	 */
	public static XmlPullParser getXML(Context context, String resource) {
		Class<R.xml> c = R.xml.class;
		XmlPullParser xpp = null;
		Field f;
		int i = 0;

		try {
			f = c.getField(resource);
			i = f.getInt(f);
			xpp = context.getResources().getXml(i);
		} catch (SecurityException e) {
			Log.e(TAG,e.getMessage());
		} catch (NoSuchFieldException e) {
			Log.e(TAG,e.getMessage());
		} catch (IllegalArgumentException e) {
			Log.e(TAG,e.getMessage());
		} catch (IllegalAccessException e) {
			Log.e(TAG,e.getMessage());
		}
		
		return xpp;
	}

	/**
	 * Answers a Drawable object for the provided resource name
	 * 
	 * @param context
	 * @param name
	 * @return
	 */
	public static Drawable getDrawable(Context context, String name) {
		Class<R.drawable> c = R.drawable.class;
		Drawable d = null;
		Field f;
		int i = 0;

		try {
			f = c.getField(name);
			i = f.getInt(f);
			d = context.getResources().getDrawable(i);
		} catch (SecurityException e) {
			Log.e(TAG,e.getMessage());
		} catch (NoSuchFieldException e) {
			Log.e(TAG,e.getMessage());
		} catch (IllegalArgumentException e) {
			Log.e(TAG,e.getMessage());
		} catch (IllegalAccessException e) {
			Log.e(TAG,e.getMessage());
		}
		
		return  d;
	}

	/**
	 * Answers a String for the provided resource name
	 * 
	 * @param context
	 * @param name
	 * @return
	 */
	public static String getString(Context context, String name) {
		Class<R.string> c = R.string.class;
		String s = null;
		Field f;
		int i = 0;

		try {
			f = c.getField(name);
			i = f.getInt(f);
			s = context.getResources().getString(i);
		} catch (SecurityException e) {
			Log.e(TAG,e.getMessage());
		} catch (NoSuchFieldException e) {
			Log.e(TAG,e.getMessage());
		} catch (IllegalArgumentException e) {
			Log.e(TAG,e.getMessage());
		} catch (IllegalAccessException e) {
			Log.e(TAG,e.getMessage());
		}
		
		return s;
	}
}
